package org.testis.orm.refinedclasses;

import java.util.HashMap;
import org.orm.PersistentException;
import org.testis.orm.classes.T_test_stepcontainer;
import org.testis.utils.middleware.TestDesign;


/**
 * 
 * @author Cahit Emir Erel
 */
public class TestSuiteRefined extends DesignPart {

    private T_test_stepcontainer m_reference;

    public TestSuiteRefined(T_test_stepcontainer reference) {
	setReference(reference);
    }

    synchronized public TestSuiteRefined createRefinedDatabaseCopy()
	    throws PersistentException {
	try {
	    return new TestSuiteRefined(createDatabaseCopy());
	} catch (NullPointerException ex) {
	    return null;
	}
    }

    private T_test_stepcontainer createDatabaseCopy()
	    throws PersistentException {
	T_test_stepcontainer newContainer;
	newContainer = new T_test_stepcontainer();
	newContainer.setContainerTypeId(this.getReference()
		.getContainerTypeId());
	newContainer.setDescription(this.getReference().getDescription());
	newContainer.setName(this.getReference().getName());
	if (newContainer.save()) {
	    return newContainer;
	} else {
	    throw new PersistentException("Unable to copy this Test Suite: "
		    + this);
	}
    }

    public String toString(TestDesignTreeNode treeNode) {
	try {
	    StringBuffer buffer = new StringBuffer();
	    buffer.append(this.toString());
	    return buffer.toString();
	} catch (NullPointerException ex) {
	    return null;
	}
    }

    @Override
    public String toString() {
	try {
	    StringBuffer buffer = new StringBuffer();
	    buffer.append("Step Container Id: " + this.getId());
	    buffer.append("\nType: "
		    + TestDesign.getDesignPartTypeName(this.getReference()
			    .getContainerTypeId()));

	    buffer.append("\nName: " + this.getName());
	    buffer.append("\nDescription: "
		    + this.getReference().getDescription());
	    return buffer.toString();
	} catch (NullPointerException ex) {
	    return null;
	}
    }

    public T_test_stepcontainer getReference() {
	return m_reference;
    }

    private void setReference(T_test_stepcontainer reference) {
	if (reference == null) {
	    throw new NullPointerException();
	}
	m_reference = reference;
    }

    @Override
    public boolean delete() {
	TestDesign.newInstance().removeTestSuite(getName());
	return super.delete();
    }

    public String getTypeName() {
	return TestDesign.getDesignPartTypeName(this.getReference()
		.getContainerTypeId());
    }

    public int compare(Comparable source) {
	return Comparable.EXACT;
    }

    public int compare(Comparable source, Comparable treeReference) {
	TestDesignTreeNode src = (TestDesignTreeNode) source;
	return compare(src.getObject());
    }

    private boolean hasSameStaticParameters(HashMap<String, Object> params,
	    Comparable treeTreference) {
	String description = (String) params.get("description");
	if (getReference().getDescription().equals(description)) {
	    return true;
	} else {
	    return false;
	}
    }

    public boolean compareParameters(HashMap<String, Object> params,
	    Comparable treeTreference) {
	return hasSameStaticParameters(params, treeTreference);
    }

    public boolean hasSameRunTimeParams(HashMap<String, Object> params,
	    Comparable treeTreference) {
	return true;
    }
}
